package com._dev.social.controller;

import com._dev.social.common.BaseResponse;
import com._dev.social.common.ErrorCode;
import com._dev.social.common.ResultUtils;
import com._dev.social.mode.domain.request.CommentsRequest;
import com._dev.social.mode.domain.response.CommentWithUserDTO;
import com._dev.social.service.CommentsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("api/comments")
public class CommentController {
    @Autowired
    private CommentsService commentsService;

    // 添加评论
    @PostMapping("/add")
    public BaseResponse<String> addComment(@RequestBody CommentsRequest comment) {
        if (Objects.isNull(comment) || comment.getContent().isEmpty() || comment.getTid().isEmpty() || comment.getUid().isEmpty()) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        commentsService.saveComment(comment);
        return ResultUtils.succeed("添加成功");
    }

    //获取当前用户所有话题的所有评论
    @PostMapping("/getAllThreadCommentByUserId")
    public BaseResponse<List<CommentWithUserDTO>> getAllThreadCommentByUserId(@RequestParam String userId) {
        if (StringUtils.isEmpty(userId)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        List<CommentWithUserDTO> commentByUserId = commentsService.getAllThreadCommentByUserId(userId);
        return ResultUtils.succeed(commentByUserId);
    }


    @GetMapping("/post")
    public BaseResponse<List<CommentWithUserDTO>> getCommentsByPost(@RequestParam Long tid) {
        if (tid == null || tid <= 0) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        List<CommentWithUserDTO> comments = commentsService.getCommentsWithUserByPostId(tid);
        return ResultUtils.succeed(comments);
    }

}
